#!/usr/bin/env python
# encoding=utf-8

import lxml.etree as et
import sys

### Class to preform XSLTransofmation
class Transformer(object):
    def transform(self, path_to_xml, path_to_xslt):
        # prepare XML parser with intensive recover for bad XMLs
        parser = et.XMLParser(recover=True)
        # create dom structure out of xml file
        dom = et.parse(path_to_xml, parser)
        # create dom structure out of xslt stylesheet
        xslt = et.parse(path_to_xslt)
        # prepare transformation
        transform = et.XSLT(xslt)
        # transform
        newdom = transform(dom)
        # print result to stdout
        print(et.tostring(newdom, pretty_print=True, encoding="UTF-8", xml_declaration=True))

if __name__ == "__main__":
    # show help
    if len(sys.argv) != 3:
        print "Usage: python transformer.py PATH_TO_XML PATH_TO_XSLT"
        sys.exit(1)
    t = Transformer()
    t.transform(sys.argv[1], sys.argv[2])
